<template>
  <BasicModal v-bind="$attrs" @register="registerModal" :title="title" :ok-text="okText" @ok="handleSubmit" width="750px">
    <a-form
      ref="formRef"
      class="antd-modal-form"
      :labelCol="{ xs: { span: 24 }, sm: { span: 5 } }"
      :wrapperCol="{ xs: { span: 24 }, sm: { span: 16 } }"
    >
      <a-row>
        <a-col :span="24">
          <a-form-item label="临时任务单" v-bind="validateInfos.entrustId">
            <JDictSelectTag v-model:value="formData.entrustId" dictCode="lab_wtemporarily,ls_name,id" :show-choose-option="true" />
          </a-form-item>
        </a-col>
       <a-col :span="24">
          <a-form-item label="实验项目名称" v-bind="validateInfos.itemName">
            <a-input v-model:value="formData.itemName" :disabled="disabled" />
            <!-- <a-button style="color:rgb(80, 173, 255);left: 10px;" @click="openForm()">引入实验模板</a-button> -->
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="实验人员" v-bind="validateInfos.userName">
            <!-- <a-input v-model:value="formData.userName" :disabled="disabled"></a-input> -->
            <a-input v-model:value="formData.userName" size="large" class="input" @click="handleAuditModal2" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="复核人员" v-bind="validateInfos.auditName">
            <a-input v-model:value="formData.auditName" size="large" class="input" @click="handleAuditModal" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="备注" v-bind="validateInfos.remark">
            <a-input v-model:value="formData.remark" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </BasicModal>
  <LabTemPOD  @register="registerTenms" @getRatifyResultS2="onAuditOks1"></LabTemPOD>
  <TableUserModal @register="registerUserModal" @getAuditResult="onAuditOk" />
  <TableUserModal @register="registerUserModal2" @getAuditResult="onAuditOk2" />
</template>

<script lang="ts" setup>
  import { BasicModal, useModalInner, useModal } from '/@/components/Modal';
  import { ref, reactive } from 'vue';
  import { Form } from 'ant-design-vue';
  import JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';
  import { saveOrUpdate } from '../LabWtemporarilyItem.api';
  import { useUserStore } from '/@/store/modules/user';
  import LabTemPOD from '../../wscientific_item/components/LabTemPOD.vue';
  import TableUserModal from '../../wscientific_item/components/LabUserTableList.vue';
  const userStore = useUserStore();
  const title = ref<string>('新增-临时任务');
  const okText = ref<string>('下一步');
 // const okText = ref<string>('确认');
  const formRef = ref();
  const disabled = ref(false);
  const emits = defineEmits(['success', 'mainTableReload']);
  const useForm = Form.useForm;
  const formData = reactive<Record<string, any>>({
    id: '',
    itemName: '',
    basisName: '',
    basisNum: '',
    basisScope: '',
    basisAtta: '',
    bigTypeName: '',
    midTypeName: '',
    team: '',
    userId: '',
    userName: '',
    auditName: '',
    auditId: '',
    itemStatus: '待分配',
    itemFinishTime: '',
    entrustType: '临时任务',
    entrustId: '',
    remark: '',
  });
  const [registerUserModal, { openModal: openUModal }] = useModal();
  const [registerUserModal2, { openModal: openUModal2 }] = useModal();
  const [registerTenms, { openModal: openUModals1 }] = useModal();
  function handleAuditModal() {
    openUModal(true, {
      oper: 'audit',
    });
  };
  function reset() {
    formData.id = '';
    formData.itemName = '';
    formData.basisName = '';
    formData.basisNum = '';
    formData.basisScope = '';
    formData.basisAtta = '';
    formData.bigTypeName = '';
    formData.midTypeName = '';
    formData.team = '';
    formData.userId = '';
    formData.userName = '';
    formData.auditName = '';
    formData.auditId = '';
    formData.itemStatus = '待分配';
    formData.itemFinishTime = '';
    formData.entrustType = '临时任务';
    formData.entrustId = '';
    formData.remark = '';

  }

  //表单验证
  const validatorRules = {
    entrustId: [{ required: true, message: '请选择临时任务单!' }],
    itemName: [{ required: true, message: '请输入项目名称!' }],
    userName: [{ required: true, message: '请选择实验人员!' }],
    auditName: [{ required: true, message: '请选择复核人员!' }],
  };
  const { validate, validateInfos } = useForm(formData, validatorRules, { immediate: true });

  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
    setModalProps({ confirmLoading: false });
    reset();
    if (data.bool) {
      title.value = '编辑 - 临时任务';
      Object.assign(formData, data.record);
      disabled.value = true;
      okText.value = '确认';
    } else {
      title.value = '新增-临时任务';
      disabled.value = false;
      okText.value = '下一步';
      formData.entrustId = '';
      formData.userId = userStore.getUserInfo.id;
      formData.userName = userStore.getUserInfo.realname;
    }
  });
   //打开检测人员列表
   function handleAuditModal2(){
    openUModal2(true, {
      oper: 'audit',
    });
  }
 //打开选择模板
 function openForm(){
    openUModals1(true, {
      oper: 'audit',
    });
  };
  // 选择检测人员
  function onAuditOk2(uId, uName) {
    formData.userId = uId;
    formData.userName = uName;
  }
 // 选择复核人员
 function onAuditOk(uId, uName) {
    formData.auditId = uId;
    formData.auditName = uName;
  };
   // 选择模板
   function onAuditOks1(uId, uName) {
    formData.itemId = uId;
    //formData.tempName = uName;
    formData.itemName =uName;
  };
  async function handleSubmit() {
    // await validate();
    // await saveOrUpdate(formData, false).then((res) => {
    //     Object.assign(formData, res.result);
    //     closeModal();
    //     emits('success', formData);
    //   });
    if (disabled.value) {
      await validate();
      await saveOrUpdate(formData, true).then((res) => {
        closeModal();
        emits('mainTableReload');
      });
    } else {
      await validate();
      await saveOrUpdate(formData, false).then((res) => {
        Object.assign(formData, res.result);
        closeModal();
        emits('success', formData);
      });
    }
  }
</script>

<style>
  /**隐藏样式-modal确定按钮 */
  .jee-hidden {
    display: none !important;
  }
</style>
